#pragma once
#include <string>
#include "Init.hpp"
extern Log lg;
Init init;
class Task
{
public: 
    Task(int sockfd, const std::string& clientip, const uint16_t& clientport)
        :sockfd_(sockfd)
        ,clientip_(clientip)
        ,clientport_(clientport)
    {}
    Task()
    {}
    void run()
    {
        char buffer[4096];
        //TCP是面向字节流的，我们怎么保证，读取上来的数据，是"一个" "完整的"报文呢？
        ssize_t n = read(sockfd_, buffer, sizeof(buffer));
        if(n > 0)
        {
            buffer[n] = 0;
            cout << "client key# " << buffer << endl;
            string echo_string = init.translation(buffer);
            //sleep(5);
            //close(sockfd_);
            //lg(Warning, "close sockfd %d done", sockfd_);
            //sleep(2);
            int m = write(sockfd_, echo_string.c_str(), echo_string.size());
            if(m < 0)
            {
               lg(Warning, "write error, errno: %d, errstring: %s", errno, strerror(errno));
            }
        }
        else if(n == 0)
        {
            lg(Info, "%s:%d quit, server close sockfd: %d", clientip_.c_str(), clientport_, sockfd_);
        }
        else 
        {
            lg(Fatal,"read error, sockfd: %d, client ip : %s , client port : %d", sockfd_, clientip_.c_str(), clientport_);
        }
        close(sockfd_);
    }
    void operator()()
    {
        run();
    }
    ~Task()
    {

    }
private:
    int sockfd_;
    std::string clientip_;
    uint16_t clientport_;
};